package main

import (
	"gitee.com/kinwyb/appTools/appTool"
	"gitee.com/kinwyb/workflow/agent/http/controller/toolGen"
	"gitee.com/kinwyb/workflow/public/log"
	"gitee.com/kinwyb/workflow/storage/sqlStroage"
	"github.com/emicklei/go-restful"
	"github.com/go-openapi/spec"
)

const appName = "workflow"

var httpPort = int64(8090)
var app *appTool.Application

func main() {
	app = appTool.NewApplication(appName)
	app.ConfigReaderInitCallback = configInit
	err := app.LoadFileConfig("cfg.json").
		EnableHttp("", int(httpPort)).
		AfterFunc(func(app *appTool.Application) {
			// 初始化http信息
			initHttp(app.Http())
			app.Http().Swagger(swaggerConfig)
		}).
		Run()
	if err != nil {
		log.Service.Fatalf("运行结束:%s", err.Error())
	}
}

func configInit(reader appTool.ConfigReader) {
	dbHost := reader.ReadString("db.host", "")
	dbUser := reader.ReadString("db.user", "")
	dbPwd := reader.ReadString("db.password", "")
	dbName := reader.ReadString("db.name", "")
	sqlStroage.InitMySQLStorage(dbHost, dbUser, dbPwd, dbName)
}

// swagger配置
var swaggerConfig = &appTool.RestfulSwaggerConfig{
	Title:    "接口文件",
	Version:  appTool.Version,
	BasePath: "/workflow",
	Host:     "workflow.bj",
	Schemes:  []string{"http"},
}

func initHttp(s *appTool.RestfulHttp) {
	ws := &restful.WebService{}
	//ws.Consumes(restful.MIME_JSON, restful.MIME_XML)
	ws.Produces(restful.MIME_JSON, restful.MIME_XML)
	//ws.Filter(apiSev.LoginFilter)
	ws.Path("/workflow/").Doc("工作流程")
	spTag := workflowRouter(ws)
	s.SwaggerTag(*spTag)
	s.AddService(ws)
}

const swaggerWorkFlowTag = "WorkFlow"

func workflowRouter(ws *restful.WebService) *spec.Tag {
	ws.Route(toolGen.FlowQuery(ws.GET("/flow/query")))
	ws.Route(toolGen.FlowQueryNodes(ws.GET("/flow/queryNodes")))
	ws.Route(toolGen.FlowSave(ws.POST("/flow/create")))
	ws.Route(toolGen.WorkCreate(ws.POST("/work/create")))
	ws.Route(toolGen.WorkFlowCreate(ws.POST("/workflow/create")))
	ws.Route(toolGen.WorkFlowQuery(ws.GET("/workflow/query")))
	ws.Route(toolGen.WorkNodeDo(ws.POST("/work/do")))
	ws.Route(toolGen.WaitDoWorkList(ws.GET("/work/waitDo")))
	ws.Route(toolGen.FlowNodeQuery(ws.GET("/flow/node/query")))
	ws.Route(toolGen.FlowNodeUpdate(ws.POST("/flow/node/update")))
	ws.Route(toolGen.FlowDel(ws.DELETE("/flow/del")))
	ws.Route(toolGen.FlowRouteQuery(ws.GET("/flow/route/query")))
	ws.Route(toolGen.EnableWorkFlow(ws.POST("/workflow/enable")))
	ws.Route(toolGen.DisableWorkFlow(ws.POST("/workflow/disable")))
	ws.Route(toolGen.WorkNodes(ws.GET("/work/nodes")))
	ws.Route(toolGen.UserGroupList(ws.GET("/workflow/userGroup/list")))
	ws.Route(toolGen.UserGroupUserIDQuery(ws.GET("/workflow/userGroup/userIDs")))
	ws.Route(toolGen.UserGroupAddUserID(ws.POST("/workflow/userGroup/userAdd")))
	ws.Route(toolGen.UserGroupDelUserID(ws.DELETE("/workflow/userGroup/userDel")))
	ws.Route(toolGen.WorkNodeQueryByID(ws.GET("/work/nodeQueryByID")))
	ws.Route(toolGen.WorkQueryByID(ws.GET("/work/queryByID")))
	// 返回tag
	return &spec.Tag{TagProps: spec.TagProps{
		Name:        swaggerWorkFlowTag,
		Description: "工作流程"}}
}
